package util.crypto

import javax.crypto.Cipher
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, InputStream, OutputStream}
import java.nio.charset.Charset


/**
 * Inspired by http://coding.westreicher.org/?p=23
 */
class RSAEncoder(keyStream: InputStream, usePrivate: Boolean)
  extends RSACoderBase(keyStream, Cipher.ENCRYPT_MODE, usePrivate) {

  def this(keyBytes: Array[Byte], usePrivate: Boolean) = this (new ByteArrayInputStream(keyBytes), usePrivate)

  def encrypt(text: String, enc: Charset): Array[Byte] = {
    val bytes = text.getBytes(enc)
    val encrypted = blockCipher(bytes)
    encrypted
  }

  def encrypt(input: InputStream, output: OutputStream) {
    val bos = new ByteArrayOutputStream
    copy(input, bos)
    bos.close
    val encrypted = blockCipher(bos.toByteArray)
  }

}





